﻿using System.Collections.Generic;
using System.Linq;

using VirtualTournaments.DataAccess.EntityManagers;
using VirtualTournaments.DataAccess.SqlServer.DataMappers;

namespace VirtualTournaments.DataAccess.SqlServer.EntityManagers
{
	internal sealed class MatchesManager : IMatchesManager
	{
		#region Singleton implementation

		private static MatchesManager _instance = null;
		private static readonly object _instanceLocker = new object();

		private MatchesManager()
		{
		}

		public static MatchesManager Instance
		{
			get
			{
				if (_instance == null)
				{
					lock (_instanceLocker)
					{
						if (_instance == null)
						{
							_instance = new MatchesManager();
						}
					}
				}
				return _instance;
			}
		}

		#endregion

		#region IMatchesManager Members

		public Business.Tournaments.Match GetByTournamentAndLocalTeamAndVisitorTeam(Business.Tournaments.Tournament tournament, Business.Tournaments.Team localTeam, Business.Tournaments.Team visitorTeam)
		{
			string tournamentName = tournament.Name;
			string localTeamName = localTeam.Name;
			string visitorTeamName = visitorTeam.Name;

			return MatchesDataMapper.Instance.GetOneAsBusinessObject
			(
				match =>
					match.JourneysMatch.Count(journeyMatch => journeyMatch.Journey.Tournament.Name == tournamentName) == 1
					&& match.LocalTeam.Name == localTeamName && match.VisitorTeam.Name == visitorTeamName
			);
		}

		#endregion

		#region IEntityManager<Match> Members

		public IEnumerable<Business.Tournaments.Match> GetAll()
		{
			throw new System.NotImplementedException();
		}

		public void Save(Business.Tournaments.Match entity)
		{
			throw new System.NotImplementedException();
		}

		public void Delete(Business.Tournaments.Match entity)
		{
			throw new System.NotImplementedException();
		}

		#endregion
	}
}
